package lagou.mode3;

import java.util.List;

public class Server {

    private List<Student> studentList;

    public Server(List<Student> studentList){
        this.studentList=studentList;
    }
    //增加学生
    public void add(){
        System.out.println("请输入要增加的学生信息：");
        System.out.println("请输入学生学号：");
        try {
            int id = ScannerStudent.getScanner().nextInt();
            if (id > 0) {
                for (Student st : studentList) {
                    if (st.getStudent_id() == id) {
                        System.out.println("该学生已存在，增加失败！");
                        return;
                    }
                }
                System.out.println("请输入学生姓名：");
                String name = ScannerStudent.getScanner().next();
                System.out.println("请输入学生年龄：");
                Student student = null;

                int age = 0;
                try {
                    age = ScannerStudent.getScanner().nextInt();
                    if (age > 0 && age < 150) {
                        student = new Student(id, name, age);
                        studentList.add(student);
                        System.out.println("成功添加学生");
                    } else {
                        throw new AgeException("年龄不合理哦");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                throw new IdException("学号不合理哦");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    //删除学生
    public void delete(){
        System.out.println("请输入要删除的学生学号：");
        int id = ScannerStudent.getScanner().nextInt();
        for (Student st : studentList) {
            if (st.getStudent_id() == id){
                studentList.remove(st);
                System.out.println("该学生信息已被成功删除");
                return;
            }

        }
        System.out.println("该学生不存在，删除失败");
    }
    //修改学生
    public void modify(){
        System.out.println("请输入要修改的学生学号：");
        int id_old = ScannerStudent.getScanner().nextInt();
        for (Student st :
                studentList) {
            if (st.getStudent_id() == id_old) {
                int index;
                index = studentList.indexOf(st);
                System.out.println("请输入新的学生学号：");
                try {
                    int id_new = ScannerStudent.getScanner().nextInt();
                    if (id_new > 0) {
                        System.out.println("请输入新的学生姓名：");
                        String name = ScannerStudent.getScanner().next();
                        System.out.println("请输入新的学生年龄：");
                        try {
                            int age = ScannerStudent.getScanner().nextInt();
                            if(age > 0 && age < 150) {
                                Student student = null;

                                student = new Student(id_new, name, age);
                                studentList.set(index, student);
                                System.out.println("学生信息修改成功！");
                                return;
                            }
                            else {
                                throw new IdException("年龄不合理哦");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    else {
                        throw new IdException("学号不合理哦");
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        System.out.println("该学生不存在，修改失败！");
    }

    public void query() {
        // 1.提示用户输入要查找的学生信息并使用变量记录
        System.out.println("请输入要查找的学生学号：");
        int id = ScannerStudent.getScanner().nextInt();

        // 2.使用增强版for循环去查找学生信息并给出提示信息
        for(Student s : studentList) {
            if(id == s.getStudent_id()){
                System.out.println("查找成功，查找到的学生信息是：" + s);
                return;
            }
        }
        // 查找失败的处理方式
        System.out.println("查找失败，该学生不存在！");
    }
    public void printStudent() {
        System.out.println("---------------------------------------------");
        System.out.println("目前所有的学生信息是：");
        for(Student s : studentList){
            System.out.println(s);
        }
        System.out.println("---------------------------------------------");
    }

}
